//
//  VideoViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "VideoViewController.h"
#import "VideoTableViewCell.h"

@interface VideoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *videoArray;
@end

@implementation VideoViewController
- (NSMutableArray *)videoArray
{
    if (!_videoArray) {
        _videoArray = [[NSMutableArray alloc]init];
    }
    return _videoArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    [self.tableView registerClass:[VideoTableViewCell class] forCellReuseIdentifier:@"videoClass"];
    [self videoUrl:kVideoUrl];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
    self.tableView.header = header;
}
- (void)headerAction
{
    [self videoUrl:kVideoUrl];
    [self.tableView.header endRefreshing];
}

- (void)videoUrl:(NSString *)url
{
    [LORequestManger GET:url success:^(id response) {
        NSArray *array = [response objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            VideoModel *model = [[VideoModel alloc]init];
            model.text = [dic objectForKey:@"text"];
            model.videouri = [dic objectForKey:@"bimageuri"];
            model.mp4_url = [dic objectForKey:@"videouri"];
            [self.videoArray addObject:model];
        }
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.videoArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"videoClass" forIndexPath:indexPath];
    VideoModel *model = _videoArray[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //[cell.playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell setValueModel:model];
    return cell;
}
- (void)playButtonAction:(NSIndexPath *)indexPath
{
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoviePlayerViewController *movieVC = [[MoviePlayerViewController alloc]init];
    VideoModel *model = [self.videoArray objectAtIndex:indexPath.row];
    movieVC.mp4_url = model.mp4_url;
    NSLog(@"model = %@",movieVC.mp4_url);
    [self presentViewController:movieVC animated:YES completion:nil];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoModel *model = [self.videoArray objectAtIndex:indexPath.row];
    VideoTableViewCell *cell = [[VideoTableViewCell alloc]init];
    return [cell height:model];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
